<script lang="ts" setup>
import { Instance } from '@/api/docker/type';
import YHeader from '@/components/YHeader.vue';
import { ref } from 'vue';
import SelectInstance from './components/SelectInstance.vue'
import SelectEnvironment from './components/SelectEnvironment.vue'
import { createUserEnvironment } from '@/api/code';
import { useUserStore } from '@/store/user';
import { Environment } from '@/api/code/environment/type';
import { router } from '@/router';
import { Result } from '@/utils/request';
import { showToast } from 'vant';

const name = ref('');
const environment = ref({} as Environment);
const instance = ref({} as Instance);

const onSubmit = () => {
    const userStore = useUserStore();
    createUserEnvironment(name.value, userStore.getUser, instance.value, environment.value)
        .then((res) => {
            const {msg} = res as Result; 
            showToast(msg);
            router.back();
        })
}

</script>

<template>
    <div class="container">
        <YHeader name="创建环境"></YHeader>
        <div class="content">
            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <van-field
                        v-model="name"
                        name="环境名"
                        label="环境名"
                        placeholder="请输入环境名"
                        :rules="[{ required: true, message: '请填写环境名' }]"
                    />
                    <SelectInstance v-model="instance"></SelectInstance>
                    <SelectEnvironment v-model="environment"></SelectEnvironment>
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit">
                    提交
                    </van-button>
                </div>
            </van-form>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.content {
    margin-top: 3%;
}
</style>